package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Questions;
import com.ebring.shareEducation.dao.QuestionsMapper;
import com.ebring.shareEducation.service.QuestionsService;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QuestionsServiceImpl implements QuestionsService {


    @Autowired
    private QuestionsMapper questionsMapper;


    @Override
    public RspMsg<Questions> selectQuestions() {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Questions> questionsList = questionsMapper.selectquestion();
        if (questionsList.isEmpty() || questionsList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }
        PageInfo<Questions> pageInfo = new PageInfo<Questions>(questionsList);
        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("questionsList", questionsList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }

    @Override
    public RspMsg<List<Questions>> findQuestions(Questions questions) {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Questions> questionList = questionsMapper.findQuestionsBy(questions);
        if (questionList.isEmpty() || questionList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关订单");
            return rspMsg;
        }

        PageInfo<Questions> pageInfo = new PageInfo<>(questionList);

        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("questionList", questionList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }
}
